package com.csdj.wenjun.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csdj.wenjun.common.CacheConstant;
import com.csdj.wenjun.common.WebResult;
import com.csdj.wenjun.common.WebResultMsgCode;
import com.csdj.wenjun.entity.Qiandao;
import com.csdj.wenjun.entity.User;
import com.csdj.wenjun.service.QiandaoService;
import com.csdj.wenjun.service.RedisService;
import com.csdj.wenjun.service.UserService;
import com.csdj.wenjun.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wenjun
 * @since 2022-01-09
 */
@RestController
@RequestMapping("/qiandao")
public class QiandaoController {
    @Resource
    private RedisService redisService;

    @Autowired
    private UserService userService;
    /**
     * 用户签到，可以补签
     *
     * @param userId  用户ID
     * @param dateStr 查询的日期，默认当天 yyyy-MM-dd
     * @return 连续签到次数和总签到次数
     */
    @ResponseBody
    @GetMapping("qd")
    public WebResult doSignIn(Integer userId, String dateStr) {
        WebResult webResult=redisService.doSign(userId, dateStr);
        if(webResult.getCode()==200){
            Qiandao qiandao= (Qiandao) webResult.getResult().get("data");
            int jifen=qiandao.getLianxu();
            if(jifen>15){
                jifen=15;
            }
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getId,userId);
            User user= userService.getOne(queryWrapper);
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id", userId);
            updateWrapper.set("jifen",user.getJifen()+jifen);
            if(userService.update(updateWrapper)){
                System.out.println("修改积分成功");
            }else {
                System.out.println("修改失败");
            }
        }
        return webResult;
    }
    @ResponseBody
    @GetMapping("selectcishu")
    public Qiandao selectcishu(Integer userId, String dateStr) {
        return redisService.selectqiandao(userId, dateStr);
    }
    @ResponseBody
    @PostMapping("upload")
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file) throws UnsupportedEncodingException {
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        String ext = filename.substring(filename.lastIndexOf("."));
        System.out.println(filename);
        long curTime = System.currentTimeMillis();
        try {
            Path path = Paths.get("d:", "upload");
            if (!Files.exists(path)) {
                Files.createDirectories(path);
            }
            file.transferTo(Paths.get("d:", "NZrengli/img", String.valueOf(curTime) + ext));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:8082/wenjun/img/"+String.valueOf(curTime) + ext;
    }
}
